package com.hqyj.system.controller;

import com.hqyj.system.model.User;
import frame.system.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName UserController
 * @Description
 * @Author zlf
 * @Date 2021/8/27 15:30
 * @Version 1.0
 **/
@Controller
@RequestMapping("/userController")
public class UserController extends BaseController {

    @RequestMapping("/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user) {
        //1.接收数据;
//        System.out.println("-----------list-------");
        //3.调用业务;
        List<User> user_dbList = userService.queryAllUser();
        System.out.println(user_dbList);
        model.addAttribute("user_dbList", user_dbList);
        //2.跳转页面;
        return "view/system/user/userList";
    }

    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user) {
        //1.接收数据;
//        System.out.println("-----------add------" + user);
        //3.调用业务;
        int n = userService.addUserByUser(user);

        //2.跳转页面;
        List<User> user_dbList = userService.queryAllUser();
        model.addAttribute("user_dbList", user_dbList);
        return "view/system/user/userList";
    }

    @RequestMapping("/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user, String idStr) {
        //1.接收数据;
        System.out.println("-----------delete------" + idStr);
        //3.调用业务;
        //字符类型转数字类型
        int userId = Integer.parseInt(idStr);
        //业务
        int n = userService.deleteUserById(userId);

        //2.跳转页面;
        List<User> user_dbList = userService.queryAllUser();
        model.addAttribute("user_dbList", user_dbList);
        return "view/system/user/userList";
    }

    @RequestMapping("/updateUI.do")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user, String idStr) {
        //1.接收数据;

        //3.调用业务;
        //字符类型转数字类型
        int userId = Integer.parseInt(idStr);
        //业务
        User user_db = userService.queryUserById(userId);
        System.out.println("-----------updateUI------" + user_db);
        model.addAttribute("user", user_db);
        //2.跳转页面;
        return "view/system/user/updateUser";
    }


    @RequestMapping("/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user, String idStr) {
        //1.接收数据;
        //字符类型转数字类型
        int userId = Integer.parseInt(idStr);

        user.setUserId(userId);
        System.out.println(user);
        //3.调用业务;
        int n = userService.updateUserByUser(user);


        //2.跳转页面;
        List<User> user_dbList = userService.queryAllUser();
        model.addAttribute("user_dbList", user_dbList);
        return "view/system/user/userList";
    }
}
